/*
		A RSS reader module for the desktop2 system, displayed scrolling vertically
		
		Author: 	Matthew Allpress
		Created:	26/06/06
		Edited:		24/09/06

*/


var FeedRequestObject = false; // XMLHttpRequest Object
var FeedBackend = 'http://localhost:5050/todo/index.php?mid=3'; // Backend url
var FeedBackendBase = 'http://localhost/todo/index.php';


var toAdd = "<div  style=\"position: absolute; overflow: hidden; overflow-x : hidden; width: 230px; height: 350px; z-index: 1; right: 0px; top: 200px; background:#FFFFFF;\" id=\"rss\">\n</div>";
toAdd += "<div  style=\"position: absolute; overflow-x : hidden; width: 230px; height: 50px; z-index: 1; right: 0px; top: 550px; background:#FFFFFF; background-color: transperant;\" id=\"rssControl\">\n";
toAdd += "\t<table width=\"100%\">\n";
toAdd += "\t<tr>\n";
toAdd += "\t\t<td><p aling=\"center\"><td align=\"center\"><input type=\"button\" value=\"||\" id=\"pause\" name=\"pause\" onClick=\"runPause();\"><input type=\"button\" value=\"\\/\" id=\"direction\" name=\"direction\" onClick=\"runDir();\"></p></td>\n";
toAdd += "\t</tr>\n";
toAdd += "\t</table>\n";
toAdd += "</div>\n";

document.body.innerHTML+= toAdd;
toAdd = null;

window.setInterval("scrollRSS()", 40);

var scrollDirection = 1;
var pauseScroll = false;
var lastScroll = 0;

window.setInterval("callUpdateRSS();", 90000);

callUpdateRSS();

function callUpdateRSS()
{
	makeNewRequest("GET", FeedBackend, FeedReqChange);
}

function runPause()
{
	if(pauseScroll == false)
	{
		pauseScroll = true;
		document.getElementById("pause").value = ">";
	}
	else
	{
		pauseScroll = false;
		document.getElementById("pause").value = "||";

	}
}

function runDir()
{
	if(scrollDirection == 1)
	{
		scrollDirection = -1;
		document.getElementById("direction").value = "/\\";
	}
	else
	{
		scrollDirection = 1;
		document.getElementById("direction").value = "\\/";
	}
}

function scrollRSS()
{
	if(!pauseScroll)
	{
		lastScroll = rss.scrollTop;
		rss.scrollTop = rss.scrollTop + scrollDirection;
	}
	
	if(rss.scrollTop == lastScroll && scrollDirection == 1)
	{
		rss.scrollTop = 0;
	}
	
	if(rss.scrollTop == 0 && scrollDirection == -1)
	{
		document.getElementById("direction").value = '\\/';
		scrollDirection = 1;
	}
}


function FeedReqChange() 
{
 var content="";

 content= '<table border=\"0\" width=\"100%\">';
 // If data received correctly
 if (XMLRequestObject.readyState == 4) 
 {
  //alert(RSSRequestObject.responseText);
  // if data is valid
  if (XMLRequestObject.responseText.indexOf('invalid') == -1) 
  { 	
   // Parsing RSS
   try
   {
	   var node = XMLRequestObject.responseXML.documentElement; 
	   // Get Channel information
	   var feeds= node.getElementsByTagName('feed');
	   
	   for (var i=0; i < feeds.length; i++)
	   {
		//var project = projects[i].getElementsByTagName('project').item(0);
		var title = feeds[i].getElementsByTagName('title').item(0).firstChild.data;
		content+= '<tr>\n<td bgcolor="#C0C0C0"><b>'+title+'</b></td>\n</tr>';
		var items = feeds[i].getElementsByTagName('item');
		
		if(items.length > 0)
		{
		 for (var n=0; n < items.length; n++)
		 { 
		  var itemTitle = items[n].getElementsByTagName('title').item(0).firstChild.data;
		  var itemLink = items[n].getElementsByTagName('link').item(0).firstChild.data;	
		  content += '<tr>\n<td><a href="' + itemLink + '">' +itemTitle+'</a></td>\n';
		 }
		}
	   }
	   content+="</table>";
	   // Display the result
		if(document.getElementById("rss").innerHTML != content)
		{
			document.getElementById("rss").innerHTML = content;
		}
		
		content = null;
		node = null;
		items = null;
		feeds = null;
		itemTitle = null;
		itemLink = null;
	}
	catch(err)
	{
		add_to_console("Error returned when calling RSS update, retrying");
		callUpdateRSS();
	}
  }
  else 
  {
   // Tell the reader that there was error requesting data
   alert("ERROR (RSS Feed): There was an error requesting the data");
  }
 }
}
